export { Tables, ifKeepKeyUnit, ifKeepImageUnit } from './base';
import { ifBase, ifKeepImageUnit } from './base';
import { MongoDBMode } from "./MongoDBMode";
import { SqliteMode } from './SqliteMode';
import { AppKeys, ifAppkeys, MongoApps } from './appkeys';
import { MongodbMoudle } from 'mx-database';

export class KeeperService {
    static binit: boolean = false;

    /**
     * @param keys 压缩使用的 apikeys
     * @param type 图片存储模式
     * @param args mongo 的时候输入 mongoIp: string, mongoPort: string, mongoDb: string
     *             sqlite 的时候输入 sqlitedbpath:string
     */
    static async init(keys: string[], type: 'mongo' | 'sqlite', ...args: string[]) {

        if (type == 'mongo') {
            this.appKeys = new MongoApps(args[2]);
            await MongodbMoudle.init([{ host: args[0], port: parseInt(args[1]) }])
            this.keeper = new MongoDBMode();
        }
        else {
            this.keeper = new SqliteMode();
        }

        if (!this.appKeys) {
            this.appKeys = new AppKeys();
        }

        await this.appKeys.init(...keys);
        await this.keeper.init(...args);
        this.binit = true;
        return this.binit;
    }

    private static keeper: ifBase;
    private static appKeys: ifAppkeys;

    static getPressKey() {
        return this.appKeys.getPressKey();
    }

    static freshAllDB() {
        return this.keeper.freshAllDB();
    }

    static async getPic(...args): Promise<ifKeepImageUnit | undefined> {
        return this.keeper.getPic(args[0]);
    }

    static async insert(f: any): Promise<void> {
        return this.keeper.inserPic(f);
    }
}


